package com.jmb.apiplatform.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jmb.apiplatform.common.PageResult;
import com.jmb.apiplatform.exception.ValidException;
import com.jmb.apiplatform.mapper.ApiCaseSuiteProcessorMapper;
import com.jmb.apiplatform.pojo.ApiCaseSuiteProcessor;
import com.jmb.apiplatform.pojo.ApiCaseSuiteProcessorTO;
import com.jmb.apiplatform.pojo.ApiCaseSuiteProcessorVO;
import com.jmb.apiplatform.service.ApiCaseSuiteProcessorService;
import com.jmb.apiplatform.util.ValidUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ApiCaseSuiteProcessorServiceImpl implements ApiCaseSuiteProcessorService {
    @Autowired
    private ApiCaseSuiteProcessorMapper apiCaseSuiteProcessorMapper;
    /**
     * 添加处理器
     * @param apiCaseSuiteProcessor 处理器
     * @return 自增
     * @throws ValidException 校验参数抛出异常
     */
    @Override
    public ApiCaseSuiteProcessor saveProcessor(ApiCaseSuiteProcessor apiCaseSuiteProcessor) throws ValidException {
        // 校验参数
        checkDO(apiCaseSuiteProcessor);
        // 保存数据
        apiCaseSuiteProcessorMapper.insertApiCaseSuiteProcessor(apiCaseSuiteProcessor);
        return apiCaseSuiteProcessor;
    }

    /**
     * 修改处理器
     * @param apiCaseSuiteProcessor 处理器
     * @throws ValidException 参数校验
     */
    @Override
    public void editSuitProcessor(ApiCaseSuiteProcessor apiCaseSuiteProcessor) throws ValidException {
        // 检验参数
        checkDO(apiCaseSuiteProcessor);
        // 修改
        apiCaseSuiteProcessorMapper.updateApiCaseSuiteProcessor(apiCaseSuiteProcessor);
    }

    /**
     * 查询处理器列表，不分页
     * @param caseSuiteProcessorTO 处理器
     * @return 返回不分页的列表
     * @throws ValidException 参数校验
     */
    @Override
    public List<ApiCaseSuiteProcessorVO> findAllApiCaseSuiteProcessorList(ApiCaseSuiteProcessorTO caseSuiteProcessorTO) throws ValidException {
        return apiCaseSuiteProcessorMapper.selectApiCaseSuiteProcessorList(caseSuiteProcessorTO);
    }

    @Override
    public PageResult<ApiCaseSuiteProcessorVO> findApiCaseSuiteProcessorList(ApiCaseSuiteProcessorTO caseSuiteProcessorTO) throws ValidException {
        PageHelper.startPage(caseSuiteProcessorTO.getPageNum(), caseSuiteProcessorTO.getPageSize());
        List<ApiCaseSuiteProcessorVO> list = apiCaseSuiteProcessorMapper.selectApiCaseSuiteProcessorList(caseSuiteProcessorTO);
        Page<ApiCaseSuiteProcessorVO> page = (Page<ApiCaseSuiteProcessorVO>) list;
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询处理器
     * @param id 处理器id
     * @return 处理器
     */
    @Override
    public ApiCaseSuiteProcessorVO findApiCaseSuiteProcessorById(Integer id) {
        return apiCaseSuiteProcessorMapper.selectApiCaseSuiteProcessorById(id);
    }

    /**
     * 根据测套件id查询处理器
     * @param suiteId 测套件id
     * @return 处理器
     */
    @Override
    public List<ApiCaseSuiteProcessorVO> findApiCaseSuiteProcessorBySuiteId(Integer suiteId) {
        return apiCaseSuiteProcessorMapper.selectApiCaseSuiteProcessorBySuiteId(suiteId);
    }

    /**
     * 根据id删除处理器
     * @param id 处理器id
     */
    @Override
    public void removeApiCaseSuiteProcessorById(Integer id) {
        apiCaseSuiteProcessorMapper.deleteApiCaseSuiteProcessorById(id);

    }

    /**
     * 根据测试套件编号删除
     * @param suiteId 套件id
     */
    @Override
    public void removeApiCaseSuiteProcessorBySuiteId(Integer suiteId) {
        apiCaseSuiteProcessorMapper.deleteApiCaseSuiteProcessorBySuiteId(suiteId);

    }

    private void checkDO(ApiCaseSuiteProcessor apiCaseSuiteProcessor) throws ValidException {
        Integer suiteId = apiCaseSuiteProcessor.getSuiteId();
        Integer processorType = apiCaseSuiteProcessor.getProcessorType();
        Integer type = apiCaseSuiteProcessor.getType();
        String value = apiCaseSuiteProcessor.getValue();

        ValidUtil.notNUll(suiteId, "测试套件编号不能为空");

        ValidUtil.notNUll(processorType, "处理器类型不能为空");
        ValidUtil.size(processorType, 0, 1, "处理器类型参数错误");

        ValidUtil.notNUll(type, "类型不能为空");
        ValidUtil.size(type, 0, 3, "类型参数错误");

        ValidUtil.notNUll(value, "处理器值不能为空");
        ValidUtil.length(value, 1,200, "处理器值长度不能超过200");

        if (type == 1) {
            ValidUtil.isJsonObject(value, "headers syntax error");
        } else if (type == 2) {
            ValidUtil.isJsonObject(value, "params syntax error");
        } else if (type == 3) {
            ValidUtil.isJsonObject(value, "data syntax error");
        }
    }
}
